package com.wwl.springcloud.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author wwl
 * @version 1.0
 * @date 2021/8/4 17:10
 * @description:
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {
    private Integer code;
    private String message;
    private T data;
    private static volatile CommonResult commonResult;
    /**
     * 线程安全获取util单例
     *
     * @return
     */
    public static CommonResult getInstance() {
        if (commonResult == null) {
            synchronized (CommonResult.class) {
                if (commonResult == null) {
                    commonResult = new CommonResult();
                }
            }
        }
        return commonResult;
    }

    public CommonResult(Integer code, String message) {
        this(code, message, null);
    }

    public CommonResult(T data) {
        this(0, "成功!", data);
    }

    public CommonResult(String message, T data) {
        this(0, message, data);
    }


    public  CommonResult success(String message) {
        return new CommonResult(0, message);
    }

    public CommonResult success(T data) {
        return new CommonResult<>(data);
    }

    public CommonResult success(String message, T data) {
        return new CommonResult<>(message, data);
    }


    public  CommonResult error(String message) {
        return new CommonResult(400, message);
    }
}
